package com.site.wisdom_construction.controller.iotModule;

import com.alibaba.fastjson.JSONObject;
import com.site.wisdom_construction.entity.iotModule.WisdomElectricityEquipment;
import com.site.wisdom_construction.entity.publicModule.Value;
import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.service.iotModule.WisdomElectricityAlarmsService;
import com.site.wisdom_construction.service.iotModule.WisdomElectricityEquipmentService;
import com.site.wisdom_construction.service.iotModule.WisdomElectricityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author daiyifei
 * @Date 2021/12/17
 * @Description
 */
@Api(tags = "智慧用电接口")
@RestController
@RequestMapping("WisdomElectricity")
@CrossOrigin
public class WisdomElectricityController {
    @Autowired
    private WisdomElectricityService wisdomElectricityService;

    @Autowired
    private WisdomElectricityAlarmsService wisdomElectricityAlarmsService;

    @Autowired
    private WisdomElectricityEquipmentService wisdomElectricityEquipmentService;

    @ApiOperation(value = "接收平台推送数据", notes = "")
    @PostMapping("/insertWisdomElectricity")
    public void insertWisdomElectricity(@RequestBody Map<String, Object> map) throws IOException {
        //map转JSONObject
        JSONObject jsonObject = new JSONObject(map);
        //获取其中的data
        JSONObject jsonObjectData = jsonObject.getJSONObject("data");
        //传参map
        Map mapR = new HashMap();
        //大桥局标段id
        mapR.put("projectId",1);
        mapR.put("deviceType", jsonObject.get("deviceType"));
        mapR.put("deviceNumber", jsonObject.get("deviceNumber"));
        mapR.put("sendTime", jsonObject.get("sendTime"));
        mapR.put("dataType", jsonObjectData.get("dataType"));
        mapR.put("aVol", jsonObjectData.get("aVol"));
        mapR.put("bVol", jsonObjectData.get("bVol"));
        mapR.put("cVol", jsonObjectData.get("cVol"));
        mapR.put("aEle", jsonObjectData.get("aEle"));
        mapR.put("bEle", jsonObjectData.get("bEle"));
        mapR.put("cEle", jsonObjectData.get("cEle"));
        mapR.put("surplusEle", jsonObjectData.get("surplusEle"));
        mapR.put("aTemp", jsonObjectData.get("aTemp"));
        mapR.put("bTemp", jsonObjectData.get("bTemp"));
        mapR.put("cTemp", jsonObjectData.get("cTemp"));
        mapR.put("nTemp", jsonObjectData.get("nTemp"));
        mapR.put("powerFactor", jsonObjectData.get("powerFactor"));
        mapR.put("gridFrequency", jsonObjectData.get("gridFrequency"));
        mapR.put("positiveActiveEnergy", jsonObjectData.get("positiveActiveEnergy"));
        mapR.put("positiveReactiveEnergy", jsonObjectData.get("positiveReactiveEnergy"));
        mapR.put("alarmStatus", jsonObjectData.get("alarmStatus"));
        mapR.put("gateStatus", jsonObjectData.get("gateStatus"));
        mapR.put("environmentTemp", jsonObjectData.get("environmentTemp"));

        //判断智慧用电数据是否添加成功
        if(jsonObjectData.get("dataType").equals("basic")){
            int i = wisdomElectricityService.insertWisdomElectricity(mapR);
            if(i==1){
                //增加告警记录
                wisdomElectricityAlarmsService.insertWisdomElectricityAlarms(mapR);
                //查询智慧用电设备
                WisdomElectricityEquipment wisdomElectricityEquipment = wisdomElectricityEquipmentService.findWisdomElectricityEquipmentByDeviceNumber(mapR);
                //判断设备是否存在
                if(wisdomElectricityEquipment==null){
                    //设备不存在添加设备
                    wisdomElectricityEquipmentService.insertWisdomElectricityEquipment(mapR);
                }else {
                    //设备存在修改设备合闸状态
                    wisdomElectricityEquipmentService.updateWisdomElectricityEquipmentByGateStatus(mapR);
                }
            }
        }else {
            System.out.println("空值不需要添加数据库");
        }


    }

    @ApiOperation(value = "查询最新数据", notes = "")
    @PostMapping("/findWisdomElectricityByNew")
    public OneDataResult findWisdomElectricityByNew(@RequestBody Map<String, Object> map) {
        OneDataResult oneDataResult = new OneDataResult();
        oneDataResult.setData(wisdomElectricityService.findWisdomElectricityByNew(map));
        return oneDataResult;
    }

    @ApiOperation(value = "电量统计", notes = "")
    @PostMapping("/countWisdomElectricity")
    public OneDataResult countWisdomElectricity(@RequestBody Map<String, Object> map) {
        OneDataResult oneDataResult = new OneDataResult();
        List<Value> valueList = wisdomElectricityService.countWisdomElectricity(map);
        List<Value> valueListQ = wisdomElectricityService.countWisdomElectricityD(map);
        List<Value> values = new ArrayList<>();
        for (int i = 0;i<valueList.size();i++){
            Value value = new Value();
            value.setMonth(valueList.get(i).getMonth());
            Double h = Double.valueOf(valueList.get(i).getValue());
            Double q = Double.valueOf(valueListQ.get(i).getValue());
            String z = String.valueOf(h-q);
            if (z.length()>3){
                String zz = z.substring(0,z.indexOf(".")+2);
                value.setValue(zz);
                values.add(value);
            }else {
                value.setValue(z);
                values.add(value);
            }

        }
        oneDataResult.setData(values);
        return oneDataResult;
    }

    @ApiOperation(value = "删除智慧用电数据信息", notes = "测试数据:")
    @PostMapping("/deleteWisdomElectricity")
    public JsonResult deleteWisdomElectricity(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        List<Integer> list = (List<Integer>) map.get("list");
        int l = 0;
        Map mapR = new HashMap();
        for (int i = 0;i<list.size();i++){
            mapR.put("id",list.get(i));
            int a = wisdomElectricityService.deleteWisdomElectricity(mapR);
            l+=a;
        }
        if (l>=1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功删除智慧用电数据信息!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除智慧用电数据信息失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页模糊查询用电量数据", notes = "测试数据:")
    @PostMapping("/findWisdomElectricityByPage")
    public Page findWisdomElectricityByPage(@RequestBody Map map) {
        Page page = new Page();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(wisdomElectricityService.Total(map));
        page.setItems(wisdomElectricityService.findWisdomElectricityByPage(map));
        return page;
    }
}
